<?php
namespace app\admin\controller\system;
use app\common\annotation\NodeAnnotation;
use app\common\controller\Admin;
use think\Request;

/**
 * 角色管理
 * Class Role
 * @package app\admin\controller\system
 * @NodeAnnotation(
 *     title="角色管理",
 *     is_menu=0,
 *     is_view=0
 * )
 */
class Role extends Admin
{
    public $model;

    public function initialize()
    {
        parent::initialize();
        $this->model= new \app\admin\model\system\Role();
    }

    /**
     * @NodeAnnotation(
     *     title="角色列表",
     *     parent="admin/system.Role",
     *     MenuRoot="admin/system.setting"
     * )
     */
    public function index(Request $request)
    {
        $keywords  = $request->param("keywords");

        $where = [];

        if($keywords) array_push($where,['title',"like","%$keywords%"]);

        $this->assign([
            "roleLists" => $this->model->where($where)->paginate($where),
            "search"    => [
                "keywords" => $keywords
            ]
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="添加角色",
     *     parent="admin/system.Role",
     *     MenuRoot="admin/system.setting",
     *     is_menu=0
     * )
     */
    public function addrole(Request $request)
    {
        if($request->isAjax())
        {
            $input = $request->post();

            $result = $this->model->createRole($input);

            if($result)
            {
                $this->success("添加成功",url('admin/system.role/index'));
            }else{
                $this->error("添加失败");
            }
        }

        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="编辑角色",
     *     parent="admin/system.Role",
     *     MenuRoot="admin/system.setting",
     *     is_menu=0
     * )
     */
    public function editrole(Request $request)
    {
        $id = $request->param("id",0,'intval');

        if($request->isAjax())
        {
            $input = $request->post();

            $result = $this->model->updateRole($input);

            if($result)
            {
                $this->success("修改成功",url('admin/system.role/index'));
            }else{
                $this->error("修改失败");
            }
        }

        $this->assign([
            "role" => $this->model->findRole($id)
        ]);

        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="编辑角色",
     *     parent="admin/system.Role",
     *     MenuRoot="admin/system.setting",
     *     is_menu=0,
     *     is_view=0
     * )
     */
    public function delrole(Request $request)
    {
        $id = $request->param("idList",0,'intval');

        if($request->isAjax())
        {
            $result = $this->model->deleteRole($id);

            if($result)
            {
                $this->success("删除成功",url('admin/system.role/index'));
            }else{
                $this->error("删除失败");
            }
        }
    }

    /**
     * @NodeAnnotation(
     *     title="权限配置",
     *     parent="admin/system.Role",
     *     MenuRoot="admin/system.setting",
     *     is_menu=0,
     *     is_view=0
     * )
     */
    public function setaccess(Request $request)
    {

        if($request->isAjax() and $request->isPost())
        {
            $input = $request->post();
            $input['rules'] = array_unique($input['rules']);
            $input['rules'] = implode(",",$input['rules']);
            $res = $this->model->updateRole($input);

            if($res)
            {
                $this->success("配置成功");
            }else{
                $this->error("配置失败");
            }
        }

        $id = $request->param("id",0,'intval');

        $role = $this->model->findRole($id);

        $nodeList = (new \app\admin\model\system\Node())->select();

        $role['rules'] = explode(',',$role['rules']);

        foreach ($nodeList as $k => $item)
        {
            if(in_array($item['id'],$role['rules']))
            {
                $nodeList[$k]['checked'] = true;
            }else{
                $nodeList[$k]['checked'] = false;
            }
        }
        return json($nodeList);

    }

}